package com.shaoshaohuo.app.view;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.StringHelper;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Description:
 */
public class MainActionPop {

    private Context context;
    private PopupWindow hintPopWinwow;

    private OnSelectItemListener onSelectItemListener;

    private ListView listView;
    private String[] actions;

    public MainActionPop(Context context, String[] actions) {
        this.context = context;
        this.actions = actions;
        initView();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View contentView = inflater.inflate(R.layout.select_flow_dialog, null, false);
        hintPopWinwow = new PopupWindow(contentView, DeviceHelper.getScreenWidth((Activity) context) / 4, WindowManager.LayoutParams.WRAP_CONTENT,
                        true);
        hintPopWinwow.setFocusable(false);
        hintPopWinwow.setOutsideTouchable(true);
        hintPopWinwow.setBackgroundDrawable(new BitmapDrawable());
//        hintPopWinwow.setAnimationStyle(R.style.Anim_Dialog_Bottom);

        listView = (ListView) contentView.findViewById(R.id.listview_flow_list);

        FlowAdapter adapter = new FlowAdapter(context, actions);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onSelectItemListener != null) {
                    onSelectItemListener.onSelect(position);
                }
                hintPopWinwow.dismiss();
            }
        });

    }

    public void showLocation(View parent) {
        int[] location = new int[2];

        parent.getLocationInWindow(location);
        hintPopWinwow.showAtLocation(parent, Gravity.LEFT | Gravity.TOP, (int) parent.getX(),
                        location[1]-StringHelper.dip2px(context, actions.length*45.5));
        // hintPopWinwow.showAtLocation(parent, Gravity.re, 0, 0);
    }

    public boolean isShowing() {
        return hintPopWinwow.isShowing();
    }

    public void setOnDismissListener(OnDismissListener onDismissListener){
        hintPopWinwow.setOnDismissListener(onDismissListener);
    }
    
    public void setOnSelectItemListener(OnSelectItemListener onSelectDateListener) {
        this.onSelectItemListener = onSelectDateListener;
    }

    public interface OnSelectItemListener {

        void onSelect(int pos);
    }
    
    class FlowAdapter extends BaseAdapter {

        private Context context;
        private String[] titles;

        public FlowAdapter(Context context, String[] actions) {
            this.context = context;
            this.titles = actions;
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int position) {
            return titles[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = View.inflate(context, R.layout.view_select_flow_item, null);
            TextView tv = (TextView) convertView.findViewById(R.id.textview_flow);
            tv.setText(titles[position]);
            return convertView;
        }

    }

}
